package controller

import (
	"context"
	"github.com/gin-gonic/gin"
	log "github.com/micro/go-micro/v2/logger"
	"web-service/api/entity"
	"web-service/api/middleware"
	"cirrus-core/structs"
	"cirrus-core/validate"
	"rpc-service/user/proto/user"
)

type UserController struct {
	BaseController
}

func (ctrl *UserController) Register(ctx *gin.Context) {
	var register entity.RegisterReq
	err := validate.Check(ctx, &register)
	if err != nil {
		//如果不合法，响应错误码和信息
		ctrl.ResponseFailed(ctx, 4000, err.Error())
		return
	}
	var req user.RegisterReq
	structs.CopyProperties(&register, &req)

	rpc := middleware.GetUserRpcClient()
	res, _ := rpc.Register(context.Background(), &req)
	log.Info("Register resp ", res)
	ctrl.ResponseData(ctx, res.User)
}

func (ctrl *UserController) Login(ctx *gin.Context) {
	var login entity.LoginReq
	err := validate.Check(ctx, &login)
	if err != nil {
		//如果不合法，响应错误码和信息
		ctrl.ResponseFailed(ctx, 4000, err.Error())
		return
	}

	var req user.LoginReq
	//var resp  user.LoginResp
	structs.CopyProperties(&login, &req)

	rpc := middleware.GetUserRpcClient()
	res, err := rpc.Login(context.Background(), &req)
	if err!=nil {
		log.Error(err.Error())
	}
	log.Info("Login resp ", res)
	ctrl.ResponseData(ctx, res)
}
